package com.api.invitation.getinvitations;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IInvitationApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.InvitationApi;
import com.zyncro.api.model.InvitationApiType;
import com.zyncro.api.model.InvitationRoleApiType;
import com.zyncro.api.model.InvitationStateApiType;
import com.zyncro.api.model.PagedItemsResultApi;

@ContextConfiguration(locations = { "classpath:/invitation/invitation-context.xml" })
public class GetInvitations extends AbstractApiTest {

	@Value("#{org_invitations[toGet].appId}")
	private String userId;

	@Value("#{org_invitations[toGet].password}")
	private String userPassword;

	@Value("#{user_toGet.cantInvitationsAll}")
	private int totalAllInvitations;

	@Value("#{user_toGet.cantInvitationsExternal}")
	private int totalExternalInvitations;

	@Value("#{user_toGet.cantInvitationsFollower}")
	private int totalFollowerInvitations;

	@Value("#{user_toGet.cantInvitationsGroup}")
	private int totalGroupInvitations;

	@Value("#{user_toGet.cantInvitationsWaiting}")
	private int totalWaitingInvitations;

	@BeforeClass
	public void setUpVariables() {

		getApi().login(userId, userPassword);
	}

	@Test
	public void testGetInvitationsAll() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<InvitationStateApiType> invitationStates = null;
		List<InvitationApiType> invitationTypes = null;
		InvitationRoleApiType invitationRole = InvitationRoleApiType.ALL;

		PagedItemsResultApi<InvitationApi> result = getApi().getService(IInvitationApiService.class).getInvitations(pageNumber,
				itemsPerPage, invitationStates, invitationTypes, invitationRole);

		Assert.assertEquals(result.getTotalItems(), totalAllInvitations, "Total number of invitations");
	}

	@Test
	public void testGetInvitationsExternal() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<InvitationStateApiType> invitationStates = null;

		List<InvitationApiType> invitationTypes = new ArrayList<InvitationApiType>();
		invitationTypes.add(InvitationApiType.EXTERNAL_CONTACT);

		InvitationRoleApiType invitationRole = InvitationRoleApiType.ALL;

		PagedItemsResultApi<InvitationApi> result = getApi().getService(IInvitationApiService.class).getInvitations(pageNumber,
				itemsPerPage, invitationStates, invitationTypes, invitationRole);

		Assert.assertEquals(result.getTotalItems(), totalExternalInvitations, "Total number of external invitations");
	}

	@Test
	public void testGetInvitationsFollower() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<InvitationStateApiType> invitationStates = null;

		List<InvitationApiType> invitationTypes = new ArrayList<InvitationApiType>();
		invitationTypes.add(InvitationApiType.FOLLOW_USER);

		InvitationRoleApiType invitationRole = InvitationRoleApiType.ALL;

		PagedItemsResultApi<InvitationApi> result = getApi().getService(IInvitationApiService.class).getInvitations(pageNumber,
				itemsPerPage, invitationStates, invitationTypes, invitationRole);

		Assert.assertEquals(result.getTotalItems(), totalFollowerInvitations, "Total number of follower invitations");
	}

	@Test
	public void testGetInvitationsGroup() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<InvitationStateApiType> invitationStates = null;

		List<InvitationApiType> invitationTypes = new ArrayList<InvitationApiType>();
		invitationTypes.add(InvitationApiType.GROUP);

		InvitationRoleApiType invitationRole = InvitationRoleApiType.ALL;

		PagedItemsResultApi<InvitationApi> result = getApi().getService(IInvitationApiService.class).getInvitations(pageNumber,
				itemsPerPage, invitationStates, invitationTypes, invitationRole);

		Assert.assertEquals(result.getTotalItems(), totalGroupInvitations, "Total number of group invitations");
	}

	@Test
	public void testGetInvitationsWaiting() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<InvitationStateApiType> invitationStates = new ArrayList<InvitationStateApiType>();

		invitationStates.add(InvitationStateApiType.WAITING_RESPONSE);

		List<InvitationApiType> invitationTypes = null;

		InvitationRoleApiType invitationRole = InvitationRoleApiType.ALL;

		PagedItemsResultApi<InvitationApi> result = getApi().getService(IInvitationApiService.class).getInvitations(pageNumber,
				itemsPerPage, invitationStates, invitationTypes, invitationRole);

		Assert.assertEquals(result.getTotalItems(), totalWaitingInvitations, "Total number of waiting invitations");
	}
}
